//
//  ZJCourseRecommendView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/30.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJCourseRecommendView.h"
#import "ZJCRImageViewAndLabelView.h"
@interface ZJCourseRecommendView ()
{
    NSArray *_seleDataArray;
}
/** 课程推荐 */
@property (nonatomic, strong) UIButton *button1;
/** 猜你喜欢 */
@property (nonatomic, strong) UIButton *button2;
/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ZJCourseRecommendView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.button1];
        [self addSubview:self.button2];
        [self addSubview:self.scrollView];
        
        self.button1.sd_layout
        .topSpaceToView(self, 0)
        .leftSpaceToView(self, 0)
        .widthIs([UIScreen zj_screenWith] / 2)
        .heightIs(44 * [UIScreen zj_screenHScale]);
        
        self.button2.sd_layout
        .topEqualToView(self.button1)
        .leftSpaceToView(self.button1, 0)
        .widthRatioToView(self.button1, 1)
        .heightRatioToView(self.button1, 1);
        
        self.scrollView.sd_layout
        .topSpaceToView(self.button1, 0)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .bottomSpaceToView(self, 0);
    }
    return self;
}
#pragma mark - 点击事件
- (void)action_button:(UIButton *)button{
    if (button.selected) {
        return;
    }
    button.selected = YES;
    [button setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
    _seleDataArray = self.dataArray[button.tag];
    if (button == self.button1) {
        self.button2.selected = NO;
        [self.button2 setTitleColor:[UIColor zj_colorWithHexString:@"898989"] forState:UIControlStateNormal];
    }else{
        self.button1.selected = NO;
        [self.button1 setTitleColor:[UIColor zj_colorWithHexString:@"898989"] forState:UIControlStateNormal];
    }
    [self updateScrollView_dataArray:_seleDataArray];
}




- (void)updateScrollView_dataArray:(NSArray *)dataArray{

    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[ZJCRImageViewAndLabelView class]]) {
            [view removeFromSuperview];
        }
    }
    
    CGFloat viewW = 140 * [UIScreen zj_screenWScale];
    CGFloat viewH = self.scrollView.height;
    CGFloat space = 10 * [UIScreen zj_screenWScale];
    for (int i = 0; i < _seleDataArray.count; i ++) {
        ZJCRImageViewAndLabelView *view = [[ZJCRImageViewAndLabelView alloc]initWithFrame:CGRectMake(i * (viewW + space) + space, 0, viewW, viewH)];
        view.delegate = self.delegate;
        view.model = _seleDataArray[i];
        
        [self.scrollView addSubview:view];
        self.scrollView.contentSize = CGSizeMake(view.right + space, self.scrollView.height);
    }
}



#pragma mark - Setter
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
   
    if (self.button1.selected) {
        _seleDataArray = dataArray[0];
    }else{
        _seleDataArray = dataArray[1];
    }
    [self updateScrollView_dataArray:_seleDataArray];
}

#pragma mark - Getters
- (UIButton *)button1{
    if (_button1 == nil){
        _button1 = [UIButton new];
        [_button1 setTitle:@"课程推荐" forState:UIControlStateNormal];
        [_button1 setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        [_button1 addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
        _button1.titleLabel.font = [UIFont titleTextFont];
        _button1.selected = YES;
        _button1.tag = 0;
    }
    return _button1;
}

- (UIButton *)button2{
    if (_button2 == nil){
        _button2 = [UIButton new];
        [_button2 setTitle:@"猜你喜欢" forState:UIControlStateNormal];
        [_button2 setTitleColor:[UIColor zj_colorWithHexString:@"898989"] forState:UIControlStateNormal];
        [_button2 addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
        _button2.titleLabel.font = [UIFont titleTextFont];
        _button2.tag = 1;
    }
    return _button2;
}

- (UIScrollView *)scrollView{
    if (_scrollView == nil){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 150)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = _scrollView.size;
    }
    return _scrollView;
}


@end
